<template>
  <div class="app-container">
<!-- 查询条件区域-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-="width:80px">
      <el-form-item label="机器人编号" prop="robot_num">
        <el-input
          v-model="queryParams.robot_num"
          placeholder="请输入机器人编号"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
      <el-form-item label="机器人名称" prop="robot_name">
        <el-input
          v-model="queryParams.robot_name"
          placeholder="请输入机器人名称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
     <!--增删改查按钮区域-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['robot:robotconfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['robot:robotconfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['robot:robotconfig:remove']"
        >删除</el-button>
      </el-col>
  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!--  数据列表区域-->
    <el-table v-loading="loading" :data="robotconfigList" @selection-change="handleSelectionChange" :cell-style="{borderColor:'rgb(1 5 43)'}" :header-cell-style="{borderColor: 'rgb(1 5 43)'}">
      <el-table-column type="selection" align="center" />
      <el-table-column label="状态" align="center" prop="robot_state" width="70px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.robot_state==1" style="font-size: 12px!important;height: 26px!important;line-height: 26px!important;display:inline-block!important;border-radius:4px!important;background: #20c013!important;border: 1px solid #024697!important;color: #fff!important" >在线</el-tag>
          <el-tag v-else style="font-size: 12px!important;height: 26px!important;line-height: 26px!important;display:inline-block!important;border-radius:4px!important;background:#6c6b6b!important;border: 1px solid #024697!important;color: #fff!important">离线</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="机器人编号" align="center" prop="robot_num"/>
      <el-table-column label="机器人名称" align="center" prop="robot_name"  width="90px"/>
      <el-table-column label="机器人ip" align="center" prop="robot_ip"  width="120px"/>
      <el-table-column label="1号摄像头IP" align="center" prop="video_ip1"  width="120px"/>
      <!-- <el-table-column label="创建人id" align="center" prop="creater_id" width="100" />-->
      <!--<el-table-column label="前置摄像头端口号" align="center" prop="q_video_port" width="100" />-->
      <!-- <el-table-column label="前摄像头用户名" align="center" prop="q_video_name" width="130" />-->
      <el-table-column label="2号摄像头IP" align="center" prop="video_ip2"  width="120px"/>
      <el-table-column label="MAC地址" align="center" prop="mac"/>
      <el-table-column label="机器人类型" align="center" prop="robot_type" width="70px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.robot_type==0" style="background-color: rgb(14 35 86);color: #5DCBFF;border-color: #1890ff;" >普通</el-tag>
          <el-tag v-else-if="scope.row.robot_type==1" style="background-color: rgb(14 35 86);color: #5DCBFF;border-color: #1890ff;">救援</el-tag>
          <el-tag v-else-if="scope.row.robot_type==2" style="background-color: rgb(14 35 86);color: #5DCBFF;border-color: #1890ff;">防爆</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="摄像头类型" align="center" prop="camera_type" width="70px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.camera_type==0" style="background-color: rgb(14 35 86);color: #5DCBFF;border-color: #1890ff;" >大华</el-tag>
          <el-tag v-else-if="scope.row.camera_type==1" style="background-color: rgb(14 35 86);color: #5DCBFF;border-color: #1890ff;">海康</el-tag>
          <el-tag v-else-if="scope.row.camera_type==2" style="background-color: rgb(14 35 86);color: #5DCBFF;border-color: #1890ff;">吊舱</el-tag>
        </template>
      </el-table-column>
     <!-- <el-table-column label="后置摄像头端口号" align="center" prop="h_video_port" width="100" />-->
     <!-- <el-table-column label="后摄像头用户名" align="center" prop="h_video_name" width="130" />-->
     <!-- <el-table-column label="备注" align="center" prop="remark" width="100" />-->
      <el-table-column label="创建时间" align="center" prop="creater_time" width="170px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creater_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-zoom-out"
            @click="handleclosed(scope.row)"
            v-hasPermi="['robot:conn:colsejqrconn']"
          >断开</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-zoom-in"
            @click="handleopen(scope.row)"
            v-hasPermi="['robot:conn:connjqr']"
          >连接</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['robot:robotconfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['robot:robotconfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表分页区域-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" style="background:rgb(1 5 43);"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" @close="handleCancle" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-row>
<!--          <el-col :span="12">
            <el-form-item label="站点名称" prop="site_id">
              <el-select v-model="form.site_id" placeholder="请选择站点名称" clearable @change="getOptions">
                <el-option
                  v-for="item in siteList"
                  :key="item.id"
                  :label="item.site_name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item label="工艺名称" prop="process_body_id">
              <el-select v-model="form.process_body_id" clearable  placeholder="请选择工艺名称">
                <el-option
                  v-for="item in processbodyList"
                  :key="item.id"
                  :label="item.process_name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机器人" prop="robot_name">
              <el-input v-model="form.robot_name" placeholder="请输入机器人名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机器人IP" prop="robot_ip">
              <el-input v-model="form.robot_ip" placeholder="请输入机器人ip" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机器人端口号" prop="robot_port">
              <el-input v-model="form.robot_port" placeholder="请输入机器人port" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头类型" prop="camera_type">
              <el-select v-model="form.camera_type" placeholder="请选择摄像头类型" clearable  @change="getOptioncameratype">
                <el-option
                  v-for="item in cameratype"
                  :key="item.camera_type"
                  :label="item.camera_type_name"
                  :value="item.camera_type"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="1号摄像头IP" prop="video_ip1">
              <el-input v-model="form.video_ip1" placeholder="请输入1号摄像头IP" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="1号摄像头端口号" prop="video_port1">
              <el-input v-model="form.video_port1" placeholder="请输入1号摄像头端口号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="1号摄像头用户名" prop="video_name1">
              <el-input v-model="form.video_name1" placeholder="请输入1号摄像头用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="1号摄像头密码" prop="video_pwd1">
              <el-input v-model="form.video_pwd1" placeholder="请输入1号摄像头密码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="2号摄像头IP" prop="video_ip2">
              <el-input v-model="form.video_ip2" placeholder="请输入2号摄像头IP" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="2号摄像头端口号" prop="video_port2">
              <el-input v-model="form.video_port2" placeholder="请输入2号摄像头端口号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="2号摄像头用户名" prop="video_name2">
              <el-input v-model="form.video_name2" placeholder="请输入2号摄像头用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="2号摄像头密码" prop="video_pwd2">
              <el-input v-model="form.video_pwd2" placeholder="请输入2号摄像头密码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机器人类型" prop="robot_type">
              <el-select v-model="form.robot_type" placeholder="请选择机器人类型" clearable  @change="getOptionrobotype">
                <el-option
                  v-for="item in robotype"
                  :key="item.robot_type"
                  :label="item.robot_type_name"
                  :value="item.robot_type"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="MAC" prop="mac">
              <el-input v-model="form.mac" placeholder="请输入MAC地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容(0-255)"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addRobotConfig,
  delRobotConfig,
  getRobotConfig,
  listRobotConfig,
  updateRobotConfig
} from "@/api/robot/robotconfig/robotconfig";
import {listSite} from "@/api/robot/site/site";
import {listProcessBody} from "@/api/robot/processbody/processbody";
import {jqrclosedCmd, jqropenCmd} from "@/api/robot/modbus/modbus";
import online from '@/assets/imgs/p2.png';
import offline from '@/assets/imgs/p22.png';
import "@/assets/styles/robt-bg.css";

export default {
  data() {
    return {
      onpc: online,
      offpc:offline,
      fileName: '',
      localFilePath:'',
      title1:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      robot_names:[],
      timer:null,//定时器
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 添加或修改对话框---站点表格数据
      siteList: [],
      mac:"",//mac地址
      //机器人类型
      robotype: [
        {robot_type:0,robot_type_name:"普通"},
        {robot_type:1,robot_type_name:"救援"},
        {robot_type:2,robot_type_name:"防爆"}
      ],
      //摄像头类型
      cameratype: [
        {camera_type:0,camera_type_name:"大华"},
        {camera_type:1,camera_type_name:"海康"},
        {camera_type:2,camera_type_name:"吊舱"}
      ],
      //查询条件区域-站点下拉列表数据
      siteListselc: [],
      //机器人配置表格数据
      robotconfigList:[],
      //工艺体表格数据
      processbodyList:[],
      //工艺体表格数据
      processbodyListc:[],
      //工艺体--机器人表格数据
      robotconfigListgyt:[],
      // 弹出层标题
      title: "",
      //是否显示弹出层
      open: false,
      // 是否显示弹出层
      open1: false,
      queryparm:{
        plcid:undefined,
        robotid:undefined
      },
      //查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        site_name: undefined,
        process_name: undefined,
        robot_num: undefined,
        robot_name: undefined,
        site_id: undefined,
        process_body_id: undefined,
        id: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        robot_name: [
          { required: true, message: "机器人名称不能为空", trigger: "blur" }
        ],
        robot_ip: [
          { required: true, message: "机器人ip不能为空", trigger: "blur" }
        ],
        robot_port: [
          { required: true, message: "机器人port不能为空", trigger: "blur" }
        ],
        process_body_id: [
          { required: true, message: "工艺名称不能为空"}
        ],
        robot_type: [
          { required: true, message: "机器人类型不能为空"}
        ],
        camera_type: [
          { required: true, message: "摄像头类型不能为空"}
        ]
      }
    };
  },
  created() {
    this.getList();    /** 初始化机器人列表 */
  },
  mounted() {
    this.timer = setInterval(()=>{
     // this.getList(); //刷新站点列表
      console.log("机器人在线离线定时器")
    },6000);//6秒
  },
  beforeDestroy() {
    //清除定时器
    if(this.timer) {
      clearInterval(this.timer); //关闭
    }
  },
  methods: {
    //自定义对话框关闭方法
    handleCancle () {
      this.processbodyList=[],//工艺列表
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    //存储机器人类型
    getOptionrobotype(robot_type){
      localStorage.removeItem("robotype");//删除键值
      localStorage.setItem("robotype",robot_type);//存储值
    },
    //存储机器人类型
    getOptioncameratype(camera_type){
      localStorage.removeItem("camera_type");//删除键值
      localStorage.setItem("camera_type",camera_type);//存储值
    },
    /** 查询条件区域----查询机器人列表 */
    getOptionsgyt(id){
      console.log("站点id"+id)
      this.queryParams.process_body_id=id;//设置站点id条件，查询工艺体
      this.getListgyt();
    },
    /** 查询条件区域-----查询工艺体列表 */
    getOptionszd(id){
      console.log("站点id"+id)
      this.queryParams.site_id=id;//设置站点id条件，查询工艺体
      this.getprocessbodysList();
    },
    /** 添加或修改对话框--点击站点下拉列表，查询工艺体列表 */
    getOptions(id){
      this.queryParams.site_id=id;//设置站点id条件，查询工艺体
      this.getprocessbodyList();
    },
    /** 添加或修改对话框--查询站点列表 */
    getsiteList() {
      listSite(this.queryParams).then(response => {
        this.siteList = response.rows;
      });
    },
    /** 查询条件区域--查询新建查询站点列表 */
    getsiteselcList() {
      listSite(this.queryParams).then(response => {
        this.siteListselc = response.rows;
      });
    },
    /** 查询条件区域-----查询工艺体下拉列表 */
    getprocessbodysList() {
      this.loading = true;
      listProcessBody(this.queryParams).then(response => {
        this.processbodyListc = response.rows;
        this.loading = false;
      });
    },
    /** 查询条件区域---查询机器人配置列表 */
    getListgyt() {
      this.loading = true;
      listRobotConfig(this.queryParams).then(response => {
        this.robotconfigListgyt = response.rows;
        this.loading = false;
      });
    },
    /** 查询工艺体列表 */
    getprocessbodyList() {
      this.loading = true;
      listProcessBody(this.queryParams).then(response => {
        this.processbodyList = response.rows;
        this.loading = false;
      });
      this.queryParams.site_id = undefined;
    },
    /** 查询机器人配置列表 */
    getListsc() {
      this.loading = true;
      listRobotConfig(this.queryParams).then(response => {
        this.robotconfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询机器人配置列表 */
    getList() {
      this.loading = true;
      listRobotConfig(this.queryParams).then(response => {
        this.robotconfigList = response.rows;
        this.total = response.total;
        this.loading = false
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.queryparm.robot_ip = undefined;
      this.queryparm.robot_port = undefined;
      this.fileName = undefined;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        site_num: undefined,
        site_name: undefined,
        site_adr: undefined,
        lon: undefined,
        lat: undefined,
        creater_id:undefined,
        creater_time:undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListsc();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.site_name=null;
      this.queryParams.process_name=null;
      this.queryParams.robot_name=null;
      this.queryParams.site_id=null;
      this.queryParams.process_body_id=null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.robot_names = selection.map(item => item.robot_name)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增机器人";
      this.getprocessbodyList();    /** 添加或修改对话框--查询工艺体列表 */
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRobotConfig(id).then(response => {
        //反显的时候用，手动调用 start
        this.getOptions(response.data.site_id);
        this.form = response.data;
        this.open = true;
        this.title = "修改机器人";
        this.getsiteList();    /** 添加或修改对话框--查询站点列表 */
      });
    },
    //关闭jqr
    handleclosed(row){
      this.queryparm.robotid = row.id;
      this.queryparm.robot_port = row.robot_port;
      jqrclosedCmd(this.queryparm).then(response => {
        if(typeof(response) != null && response != "" && response.msg!="") this.$modal.msg(response.msg);
      });
    },
    //打开jqr连接
    handleopen(row){
      this.queryparm.robotid = row.id;
      this.queryparm.robot_ip = row.robot_ip;
      this.queryparm.robot_port = row.robot_port;
      jqropenCmd(this.queryparm).then(response => {
        if(typeof(response) != null && response != ""&& response.msg!="") this.$modal.msg(response.msg);
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //验证mac地址
          if(this.form.mac!=null && this.form.mac!=""){
            let temp = /^[1-9]{1,2}:[1-9]{1,2}:[1-9]{1,2}:[1-9]{1,2}:[1-9]{1,2}:[1-9]{1,2}$/;
            if (!temp.test(this.form.mac)) {
              this.$modal.msgError("MAC地址冒号分割的6组数字组成。例：11:22:33:44:55:66");
              return false;
            }
          }

          if (this.form.id != undefined) {
            updateRobotConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.queryParams.site_id=null;
              this.getList();
            });
          } else {
            addRobotConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.queryParams.site_id=null;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const robot_names =row.robot_name || this.robot_names
      this.$modal.confirm('是否确认删除机器人名称为"' + robot_names + '"的数据项？').then(function() {
      // this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delRobotConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
